package handler;

/**
 * A database Schema of the Permissions Table.  This allows
 * a row from the Table to taken from the database and
 * stored onto the active program.
 * 
 * @author David Hietpas
 */
public class Permissions
{
    
    private int NotebookFileId;
    private int userId;
    private int permissionType;
    private byte[] desKey;

    public Permissions(){}
    
    public Permissions(int NotebookFileId, int userId, int permissionType, byte[] desKey) {
        this.NotebookFileId = NotebookFileId;
        this.userId = userId;
        this.permissionType = permissionType;
        this.desKey = desKey;
    }
    
    /**
     * Returns NotebookFiles id.
     * 
     * @return int id
     */
    public int getNotebookFileId() {
        return this.NotebookFileId;
    }

    /**
     * Returns Users id.
     * 
     * @return int id
     */
    public int getUserId() {
        return this.userId;
    }
    
    /**
     * Returns Permission Type.  0 for Read, 1 for Read/Write.
     * 
     * @return int id
     */
    public int getPermissionType() {
        return this.permissionType;
    }

    /**
     * Returns desKey for User to unlock File.
     * 
     * @return byte[] desKey
     */
    public byte[] getDesKey() {
        return this.desKey;
    }
    
    /**
     * Sets Permission to ReadOnly
     * 
     */
    public void setReadOnly(){
        this.permissionType = 0;
    }
    
    /**
     * Sets Permission to ReadWriteOnly
     * 
     */
    public void setReadWriteOnly(){
        this.permissionType = 1;
    }
    
    /**
     * Sets NotebookFile Id
     * 
     * @param int id
     */
    public void setNotebookFileId(int id){
        this.NotebookFileId = id;
    }
    
    /**
     * Sets User Id
     * 
     * @param int id
     */
    public void setUserId(int id){
        this.userId = id;
    }
    
    /**
     * Sets Encrypted DESKey
     * 
     * @param byte[] desKey
     */
    public void setEncryptedDESKey(byte[] desKey) {
        this.desKey = desKey;
    }
    
    /**
     * If Permission exists in the Database, this method can be called
     * which will update it in the Database.
     * 
     */
    public void update() {
        DBHandler dbh = new DBHandler();
        dbh.updatePermission(this);
    }
    
    /**
     * Returns a String Representation of the Permissions Class.
     * 
     * @return String Permissions
     */
    public String toString() {
        return "{" + userId + " , " + NotebookFileId + " , " + permissionType + " , " + new String(desKey) + "}";
    }
}